import { observable, action } from 'mobx';

import * as API_ODOC_CENTER from '../../apis/centerApi';

import SearchStore from '../common/SearchStore';
import { hashHistory } from 'react-router';
import { WeaLocaleProvider } from 'ecCom';

const { getLabel } = WeaLocaleProvider;

export default class CenterStore {
  // 公文创建  大类（发文拟稿，收文登记）
  @observable createReq = {
    createReqDraft: {
      showName: getLabel('33768', '发文拟稿'),
      color: '#ffffff',
      action: 'createReqDraft',
      bgcolor: '#fe7e50',
      officalType: '1',
    },
    createReqReceipt: {
      showName: getLabel('33788', '收文登记'),
      color: '#ffffff',
      action: 'createReqReceipt',
      bgcolor: '#0066b1',
      officalType: '2',
    },
  };

  // 公文创建  子类
  @observable createOffical = [{
    createOfficalType: 1,
    createOfficalList: [],
    createOfficalListKeys: [],
  }, {
    createOfficalType: 2,
    createOfficalList: [],
    createOfficalListKeys: [],
  }];
  @observable popover1 = false;
  @observable popover2 = false;

  // 按公文种类分类统计数据
  @observable countOfOdocType = {
    // showname: "按公文种类分类",
    // totalCount: 0
  };

  // 按主题分类统计数据
  @observable countOfTopicType = {};

  // 搜索条件
  @observable searchStore = new SearchStore();

  // 公文库列表
  @observable librarys = [];

  // 待办列表
  @observable todos = [];

  // 当前选中的待办tab页
  @observable selectedTodoTab = 1;

  // tab页
  @observable tabs = [];

  // tab页统计
  @observable tabsCount = {}


  // 待阅列表
  @observable toView = [];
  // 当前选中的待阅tab页
  @observable selectedToViewTab = 1;
  // 待阅tab页
  @observable toViewTabs = [];
  // 待阅tab页统计
  @observable toViewTabsCount = {}

  @observable unSupportSPAWf = [];
  @observable showToviewComp = true;

  @action
  setCreateReq = (createReq) => {
    this.createReq = createReq;
  }
  @action
  setCreateOffical = (createOffical) => {
    this.createOffical = createOffical;
  }
  @action
  setCountOfOdocType = (countOfOdocType) => {
    this.countOfOdocType = countOfOdocType;
  }
  @action
  setCountOfTopicType = (countOfTopicType) => {
    this.countOfTopicType = countOfTopicType;
  }
  @action
  setLibrarys = (librarys) => {
    this.librarys = librarys;
  }
  @action
  setTodos = (todos) => {
    this.todos = todos;
  }
  @action
  setView = (toView) => {
    this.toView = toView;
  }

  @action
  setSelectedTodoTab = (selectedTodoTab) => {
    this.selectedTodoTab = selectedTodoTab;
  }
  @action
  setSelectedtoViewTab = (selectedToViewTab) => {
    this.selectedToViewTab = selectedToViewTab;
  }
  @action
  setTabs = (tabs) => {
    this.tabs = tabs;
  }
  @action
  setToViewTabs = (tabs) => {
    this.toViewTabs = tabs;
  }

  @action.bound
  setTabsCount = (tabsCount) => {
    this.tabsCount = tabsCount;
  }

  @action.bound
  setToViewTabsCount = (toViewTabsCount) => {
    this.toViewTabsCount = toViewTabsCount;
  }

  constructor() {
    this.searchStore.setSearchType(2);
  }

  // 构建公文创建（大类）数据
  @action.bound
  buildCreateReq (params) {
    API_ODOC_CENTER.getCenterInfo(params).then((data) => {
      this.setCreateReq(data);
    });
  }

  // 构建公文创建（子类）数据
  @action.bound
  buildCreateOffical () {
    this.buildCreateOfficalList({
      officalType: 1,
    });
    this.buildCreateOfficalList({
      officalType: 2,
    });
  }

  // 构建公文创建（子类）数据
  @action.bound
  buildCreateOfficalList (params) {
    API_ODOC_CENTER.createOfficalList(params).then((data) => {
      this.unSupportSPAWf = data.unSupportSPAWf;
      let createOfficalList = [];
      let createOfficalListKeys = [];
      data.datas.forEach((element) => {
        let createOffical = {
          childs: [],
          domid: `type_${element.id}`,
          haschild: false,
          isopen: true,
          key: element.id,
          name: element.typeName,
        };
        element.wfbeans.forEach((child) => {
          let isagent = 0;
          if (child.beagenters != '') {
            isagent = 1;
          }
          let createOfficalChild = {
            domid: `wf_${child.id}`,
            haschild: false,
            isopen: false,
            key: child.id,
            name: child.name,
            workflowid: child.id,
            isagent,
            beagenters: child.beagenters,
            belongtoUsers: child.belongtoUsers,
          };
          createOffical.childs.push(createOfficalChild);
        });

        if (createOffical.childs.length > 0) {
          createOffical.haschild = true;
        }

        createOfficalList.push(createOffical);
        createOfficalListKeys.push(createOffical.key);
      });

      let createOffical = {
        createOfficalType: params.officalType,
        createOfficalList,
        createOfficalListKeys,
      };
      if (params.officalType == 1) {
        this.createOffical[0] = createOffical;
      }
      if (params.officalType == 2) {
        this.createOffical[1] = createOffical;
      }
      // this.setCreateOffical(createOffical);
    });
  }

  // 公文创建计数
  @action.bound
  addWorkflowUseCount (wfid) {
    let params = {
      wfid,
    };
    API_ODOC_CENTER.addWorkflowUseCount(params).then(() => { });
  }

  // 构建按公文种类分类统计数据
  @action.bound
  buildCountOfOdocType (params) {
    API_ODOC_CENTER.getCountByOdocType(params).then((data) => {
      this.setCountOfOdocType(data.countByOdocType);
    });
  }

  // 构建按主题分类统计数据
  @action.bound
  buildCountOfTopicType (params) {
    API_ODOC_CENTER.getCountByTopicType(params).then((data) => {
      this.setCountOfTopicType(data.countByTopicType);
    });
  }

  @action.bound
  doSearch () {
    this.searchStore.setShowSearchAd(false);
    let param = this.searchStore.getFormParams();
    // this.buildLibrarys(param);
    const path = {
      pathname: '/main/offical/odocLibrary',
      query: param,
    };

    hashHistory.push(path);
  }

  @action.bound
  buildSearchCondition (params = {}) {
    API_ODOC_CENTER.getSearchData(params).then((data) => {
      let searchConditon = {};
      let condition = [];
      condition.push(data.condition[0].searchCondition);
      searchConditon.condition = condition;
      this.searchStore.setSearchCondition(searchConditon);

      const conditioninfo = searchConditon.condition;
      this.searchStore.form.initFormFields(conditioninfo);

      this.searchStore.setKeywords(data.condition[0].searchHotTerm.dataList);
    });
  }

  @action.bound
  getSearchsBaseValue () {
    return this.searchStore.form.getFormParams().odocSubject;
  }

  @action.bound
  setSearchsBaseValue (v) {
    this.searchStore.form.updateFields({
      odocSubject: {
        value: v,
      },
    });
    this.searchStore.isSearched();
  }

  @action.bound
  resetForm () {
    this.searchStore.form.resetForm();
    this.setSearchsBaseValue('');
    this.searchStore.isSearched();
  }

  // 构建主题库数据
  @action.bound
  buildLibrarys (params) {
    API_ODOC_CENTER.odocLibrary(params).then((data) => {
      this.setLibrarys(data.odocLibrary.generateParams.showList);
    });
  }

  // 构建待办数据
  @action.bound
  buildTodos (params) {
    // 构建除了搜索框之外的查询参数
    let officalType = this.selectedTodoTab;
    let p = {
      requestname: '',
      odoctype: '',
      topictype: '',
      dispatchnum: '',
      createdateselect: '0',
      readstatus: '',
      docnode: '',
      officalType,
      iscomplete: 0,
    };
    Object.assign(p, p, params);

    API_ODOC_CENTER.odocTodolist(p).then((data) => {
      this.setTodos(data.OdocTodolist.generateParams.showList);
    });
  }

  // 构建待办数据
  @action.bound
  buildToView (params) {
    // 构建除了搜索框之外的查询参数
    let officalType = this.selectedToViewTab;
    let p = {
      requestname: '',
      odoctype: '',
      topictype: '',
      dispatchnum: '',
      createdateselect: '0',
      readstatus: '',
      docnode: '',
      officalType,
      isview: 0,
    };
    Object.assign(p, p, params);

    API_ODOC_CENTER.odocTodolist(p).then((data) => {
      this.setView(data.OdocTodolist.generateParams.showList);
    });
  }

  // 构建待办tab页
  @action.bound
  buildTodoTabs (module, params = {}) {
    if (this.API_TABCOUNT == '') {
      return;
    }
    const p = { ...params, iscomplete: 0, isNew: false };
    API_ODOC_CENTER.getTodoCount(p).then((data) => {
      let tabArr = data.datalist;
      let tabs = [];
      let tabsCount = {};
      tabArr.forEach((element) => {
        let tab = {
          color: '#000000',
          showcount: true,
          groupid: element.officalType,
          title: `${element.title}(${element.count})`,
          viewcondition: element.officalType,
        };
        tabs.push(tab);
        tabsCount[element.officalType] = element.count;
      });
      this.setTabs(tabs);
      this.setTabsCount(tabsCount);
    });
  }

  @action.bound
  // 构建待阅tab页
  buildToViewTabs (module, params = {}) {
    if (this.API_TABCOUNT == '') {
      return;
    }
    const p = { ...params, isview: 0, isNew: false };
    API_ODOC_CENTER.getTodoCount(p).then((data) => {
      this.showToviewComp = data.showCenterRead;
      let tabArr = data.datalist;
      let tabs = [];
      let tabsCount = {};
      tabArr.forEach((element) => {
        let tab = {
          color: '#000000',
          showcount: true,
          groupid: element.officalType,
          title: `${element.title}(${element.count})`,
          viewcondition: element.officalType,
        };
        tabs.push(tab);
        tabsCount[element.officalType] = element.count;
      });
      this.setToViewTabs(tabs);
      this.setToViewTabsCount(tabsCount);
    });
  }

  @action.bound
  changeTodoTab (selectedTab) {
    this.setSelectedTodoTab(selectedTab);
    this.buildTodos();
  }

  @action.bound
  changeToViewTab (selectedTab) {
    this.setSelectedtoViewTab(selectedTab);
    this.buildToView();
  }

  @action.bound
  popoverChange (bool, key) {
    this[key] = bool;
  }
}
